﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Search;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace App15
{
    public sealed partial class MainPage : Page
    {
        public MainPage() { this.InitializeComponent(); }
    }

    sealed class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled)
                Start();
        }

        public void Start()
        {
            var accelerometer = Windows.Devices.Sensors.Accelerometer.GetDefault();
            if (accelerometer == null)
            {
                // no sensor
            }
            else
            {
                var range = default(double);
                var multiplier = default(double);
                var dispatcher = Window.Current.Dispatcher;
                Func<double, double, double, double, double> calc = (current, reading, mult, minmax) =>
                {
                    var value = current + reading * mult;
                    if (value <= -minmax)
                        return -minmax;
                    if (value >= minmax)
                        return minmax;
                    return value;
                };
                CompositionTarget.Rendering += (s, e) =>
                {
                    var reading = accelerometer.GetCurrentReading();
                    var x = reading.AccelerationX;
                    var y = -reading.AccelerationY;

                    range = 25;
                    multiplier = 10;
                    this.Level1X = calc(this.Level1X, x, multiplier, range);
                    this.Level1Y = calc(this.Level1Y, y, multiplier, range);

                    range = 50;
                    multiplier = 20;
                    this.Level2X = calc(this.Level2X, x, multiplier, range);
                    this.Level2Y = calc(this.Level2Y, y, multiplier, range);

                    range = 75;
                    multiplier = 30;
                    this.Level3X = calc(this.Level3X, x, multiplier, range);
                    this.Level3Y = calc(this.Level3Y, y, multiplier, range);
                };
            }
        }

        double _Level1X = default(double);
        public double Level1X { get { return _Level1X; } set { SetProperty(ref _Level1X, value); } }

        double _Level1Y = default(double);
        public double Level1Y { get { return _Level1Y; } set { SetProperty(ref _Level1Y, value); } }

        double _Level2X = default(double);
        public double Level2X { get { return _Level2X; } set { SetProperty(ref _Level2X, value); } }

        double _Level2Y = default(double);
        public double Level2Y { get { return _Level2Y; } set { SetProperty(ref _Level2Y, value); } }

        double _Level3X = default(double);
        public double Level3X { get { return _Level3X; } set { SetProperty(ref _Level3X, value); } }

        double _Level3Y = default(double);
        public double Level3Y { get { return _Level3Y; } set { SetProperty(ref _Level3Y, value); } }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        protected void SetProperty<T>(ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName] String propertyName = null)
        {
            if (!object.Equals(storage, value))
            {
                storage = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
        protected void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = null)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}
